package service;

import java.sql.*;

public class sqlService {
    static final String DB_URL = "jdbc:mysql://localhost:3306/livinginzhuhai?characterEncoding=UTF-8&useSSL=false";
    static final String USER = "root";
    static final String PASS = "12345678c";

    public static String login(String user, String pwd){
        Connection conn = null;
        Statement stmt = null;
        boolean flag = false;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            PreparedStatement sql;
            sql = conn.prepareStatement("SELECT * FROM `user` WHERE id = ? AND pwd = ?");
            sql.setString(1, user);
            sql.setString(2, pwd);
            ResultSet rs = sql.executeQuery();
            if(rs.next()){
                flag = true;
            }
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        if(flag){
            return "OK";
        }
        else{
            return "NO";
        }
    }
    public static String register(String user, String pwd){
        Connection conn = null;
        Statement stmt = null;
        boolean flag = false;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            PreparedStatement sql;
            if (stmt.executeQuery("SELECT * FROM user WHERE id='" + user + "'").next()){
                flag = false;
            }
            else{
                sql = conn.prepareStatement("INSERT INTO USER(id,pwd)VALUES(?,?)");
                sql.setString(1, user);
                sql.setString(2, pwd);
                sql.executeUpdate();
                flag = true;
            }
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        if(flag){
            return "OK";
        }
        else{
            return "NO";
        }
    }

    public static void write(String cmd) {
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            PreparedStatement sql;
            sql = conn.prepareStatement(cmd);
            sql.executeUpdate();
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    public static save read(String user) {
        Connection conn = null;
        Statement stmt = null;
        save res = new save();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            PreparedStatement sql = conn.prepareStatement("SELECT * FROM `save` WHERE user = ?");
            sql.setString(1, user);
            ResultSet rs;
            rs = sql.executeQuery();
            res.user = user;
            while(rs.next()){
                res.day = Integer.parseInt(rs.getString("day"));
                res.hp = Integer.parseInt(rs.getString("hp"));
                res.place = rs.getString("place");
                res.cash = Integer.parseInt(rs.getString("cash"));
                res.bank = Integer.parseInt(rs.getString("bank"));
                res.guangpanp = Integer.parseInt(rs.getString("guangpanp"));
                res.guangpanq = Integer.parseInt(rs.getString("guangpanq"));
                res.xiangyanp = Integer.parseInt(rs.getString("xiangyanp"));
                res.xiangyanq = Integer.parseInt(rs.getString("xiangyanq"));
                res.huazhuangp = Integer.parseInt(rs.getString("huazhuangp"));
                res.huazhuangq = Integer.parseInt(rs.getString("huazhuangq"));
                res.wanjup = Integer.parseInt(rs.getString("wanjup"));
                res.wanjuq = Integer.parseInt(rs.getString("wanjuq"));
                res.shoujip = Integer.parseInt(rs.getString("shoujip"));
                res.shoujiq = Integer.parseInt(rs.getString("shoujiq"));
                res.jiajiup = Integer.parseInt(rs.getString("jiajiup"));
                res.jiajiuq = Integer.parseInt(rs.getString("jiajiuq"));
                res.avp = Integer.parseInt(rs.getString("avp"));
                res.avq = Integer.parseInt(rs.getString("avq"));
                res.qichep = Integer.parseInt(rs.getString("qichep"));
                res.qicheq = Integer.parseInt(rs.getString("qicheq"));
                res.guwanp = Integer.parseInt(rs.getString("guwanp"));
                res.guwanq = Integer.parseInt(rs.getString("guwanq"));
                res.fangzip = Integer.parseInt(rs.getString("fangzip"));
                res.fangziq = Integer.parseInt(rs.getString("fangziq"));
            }
            rs.close();
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return res;
    }
}
